﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CYLScheck.UIcomm
{
    /// <summary>
    /// 通用 UI控件控制类
    /// </summary>
      public static  class UIcontorlcomm
    {
        /// <summary>
        /// 焦点不在dataGridView 键盘控制上下键 控制 dataGridView   的行上下移动
        /// </summary>
        /// <param name="e">键盘按键消息</param>
        /// <param name="dataGridView1">dataGridview对象</param>
        public static void newkeyupdown(KeyEventArgs e, DataGridView dataGridView1)
        {


            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
            {
                e.Handled = true;
                if (dataGridView1.CurrentCell == null)
                {
                    if (dataGridView1.Rows.Count > 0 && dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Visible) > 0)
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Index];
                    }
                }
                else
                {
                    DataGridViewCell cell = dataGridView1.CurrentCell;
                    int iIndex = -1;
                    if (e.KeyCode == Keys.Up)
                        iIndex = dataGridView1.Rows.GetPreviousRow(cell.RowIndex, DataGridViewElementStates.Visible);
                    else
                        iIndex = dataGridView1.Rows.GetNextRow(cell.RowIndex, DataGridViewElementStates.Visible);
                    if (iIndex >= 0)
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[iIndex].Cells[cell.ColumnIndex];
                    }
                }

            }

        }


    }
}
